var index;
var index3;
$(function(){
  initSignTypeMap();
  initTab("");
  searchBtnInit();
  doBtnInit();
});

function initSignTypeMap() {
  $.ajax({
    type: "get",
    url: "initSignTypeMap.do",
    success: function (data) {
    }
  });
}


function doBtnInit() {

  //报销表格初始化
  $("#signTableDiv").on("click",".expend",function () {
    var signId = $(this).attr("signId");
    $("#expendSignId").val(signId);
    layui.use(['layer'],function () {
      var layer = layui.layer,$=layui.$;
      index3 = layer.open({
        type:1,//类型
        area: ['auto', 'auto'],//定义宽和高
        title:'报销记录',//题目
        shadeClose:false,//点击遮罩层关闭
        zIndex: 1001,
        content: $('#layerOpen3')//打开的内容
      });
    });
    layui.use(['table'], function() {
      var table = layui.table;
      table.render({
        elem: '#expendTab'
        , url: 'getExpend.do?signId=' + signId
        , height: '290',
        page: {
          layout: ['count', 'prev', 'page', 'next', 'skip']
          , groups: 1 //只显示 1 个连续页码
          , first: false //不显示首页
          , last: false //不显示尾页
          , limit: 5
          , limits: [5]
        }
        , cols: [[
          {field: 'expendType', align: 'center', width: '20%', title: '报销类型'}
          ,{field: 'expendPrice', align: 'center', width: '20%', title: '报销金额'}
          , {field: 'expendMark', align: 'center', width: '40%',title: '备注'}
          , {
            field: 'id', align: 'center', width: '20%', title: '操作',
            templet: function (data) {
              var html = '';
              if(data.expendStatus == 4){
                html += '<a class="layui-btn layui-btn-xs" lay-event="edit" style="background-color: #1E9FFF;">审批完成</a>';
              }else if(data.expendStatus == 0){
                html += '<a class="layui-btn layui-btn-xs" lay-event="edit" style="background-color: #FF5722;">审批不通过</a>';
                html += '<a class="layui-btn layui-btn-xs deleteExpendBtn" lay-event="edit" expendId='+data.id+'>删除</a>';
              }else {
                html += '<a class="layui-btn layui-btn-xs" lay-event="edit" >审批中</a>';
              }
              return html;
            }
          }
        ]]
      });
    });
    getExpendTotal(signId);
  });


  //详情
  $("#signTableDiv").on("click",".showSign",function () {
    var signId = $(this).attr("signId");
    $("#editSignId").val(signId);
    layui.use(['layer'],function () {
      var layer = layui.layer,$=layui.$;
      index = layer.open({
        type:1,//类型
        area: ['auto', 'auto'],//定义宽和高
        title:'客户匹配信息',//题目
        shadeClose:false,//点击遮罩层关闭
        zIndex: 1001,
        content: $('#layerOpen')//打开的内容
      });
    });
    initQyTable(true);
    initMatchedTable();
    initCanMatchPersonSignTable();
  });
  
  $("#matchedTabDiv").on("click",".cancelMatch",function () {
    var personSignId = $(this).attr("personSignId");
    var signId = $("#editSignId").val();
    swal({
      title: "你确定要将该证书在当前公司移除？",
      type: "warning",
      allowOutsideClick: true,
      showConfirmButton: true,
      showCancelButton: true,
      confirmButtonClass: "layui-btn layui-btn-warm",
      cancelButtonClass: "layui-btn layui-btn-primary",
      confirmButtonText: "确认",
      cancelButtonText: "取消"
    }, function (o) {
      if (o) {
        $.ajax({
          type: "get",
          url: "cancelMatchPersonSign.do",
          data: {
            "signId": signId,
            "personSignId": personSignId
          },
          success: function (data) {
            if("success" == data){
              toastrComponent.show("success", "移除成功！");
              initMatchedTable();
              initCanMatchPersonSignTable();
              var searchStr = $("#searchStr").val();
              initTab(searchStr);
            }
          }
        });
      }
    });
  });


}

function getExpendTotal(signId) {
  $.ajax({
    type: "get",
    url: "getAllExpend.do",
    data: {
      "signId": signId
    },
    success: function (data) {
      if("" == data){
        $("#expendTotal").text("0");
      }else {
        $("#expendTotal").text(data);
      }
    }
  });
}



//搜索按钮事件
function searchBtnInit() {
	$("#searchBtn").on("click",function () {
		var serachStr = $("#searchStr").val();
    initTab(serachStr);
  });
}

//初始化表格
function initTab(search) {
  layui.use(['table'], function() {
    var table = layui.table;
    table.render({
      elem: '#signTab'
      , url: 'getCanMatchSignCustom.do?search=' + search
      , height: '480',
      page: {
        layout: ['count', 'prev', 'page', 'next', 'skip']
        , groups: 1 //只显示 1 个连续页码
        , first: false //不显示首页
        , last: false //不显示尾页
        , limit: 10
        , limits: [10]
      }
      , cols: [[
        {field: 'name', align: 'center', width: '20%', title: '名称'}
        , {field: 'expense', align: 'center', width: '10%', title: '费用报销',
          templet: function (data) {
            var html = '<a class="layui-btn layui-btn-xs expend" lay-event="edit" signId='+data.id+' >费用报销</a>';
            return html;
          }}
        , {field: 'createTime', align: 'center', width: '10%', title: '录入时间'}
        , {field: 'mark', align: 'center', width: '25%', title: '备注'}
        , {field: 'matchInfo', align: 'center', width: '20%', title: '匹配情况',templet: function (data) {
          var html = '';
          html += '<a class="layui-btn layui-btn-xs" style="background-color: #FF5722" lay-event="edit">需要证书：'+data.needCount+'</a>';
          html += '<a class="layui-btn layui-btn-xs" lay-event="edit">已匹配证书：'+data.matchCount+'</a>';
          return html;
        }}
        , {
          field: 'id', align: 'center', width: '15.3%', title: '操作',
          templet: function (data) {
            var html = '';
            html += '<a class="layui-btn layui-btn-xs showSign" lay-event="edit" signId='+data.id+' approvalState='+data.approvalState+'>匹配客户</a>';
            return html;
          }
        }
      ]]
    });
  })
}


function tablerelaod(tableId) {
  var table = layui.table;
  table.reload(tableId);
}

function initQyTable(){
  var signId = $("#editSignId").val();
  layui.use(['table'], function() {
    var table = layui.table;
    table.render({
      elem: '#qyTab'
      ,id: 'qyTab'
      , url: 'getSignType.do?signId=' + signId
      ,page: {
        layout: ['count', 'prev', 'page', 'next', 'skip']
        , groups: 1 //只显示 1 个连续页码
        , first: false //不显示首页
        , last: false //不显示尾页
        , limit: 5
        , limits: [5]
      }
      , cols: [[
        {type:'checkbox',width: '5%'}
        ,{field: 'bigType', align: 'center', width: '10%', title: '大类'}
        ,{field: 'smallType', align: 'center', width: '10%', title: '小类'}
        , {field: 'unit', align: 'center', width: '5%', title: '年限',templet: function (data) {
          var chUnit;
          if(data.unit == 1){
            chUnit="一年";
          }else if(data.unit == 2){
            chUnit="两年";
          }else if(data.unit == 3){
            chUnit="三年";
          }
          return  chUnit;
        }}
        ,{field: 'purpose', align: 'center', width: '8%', title: '用途'}
        ,{field: 'project', align: 'center', width: '10%', title: 'B证'}
        ,{field: 'initStatus', align: 'center', width: '8%', title: '状态'}
        ,{field: 'price', align: 'center', width: '9%', title: '价格'}
        , {field: 'peopleNum', align: 'center', width: '5%',title: '人数'}
        , {field: 'firstPrice', align: 'center', width: '10%', title: '预付款'}
        , {field: 'scendPrice', align: 'center', width: '10%', title: '中期款'}
        , {field: 'thirdPrice', align: 'center', width: '10.4%', title: '尾款'}
        ]]

    });

    $("#personTabDiv").on("click",".matchPersonSign",function () {
      var personSignId = $(this).attr("personSignId");
      var signId = $("#editSignId").val();
      var checkData = table.checkStatus('qyTab').data;
      if(checkData.length == 0){
        toastrComponent.show("warning", "请选择一个需要匹配的证书！");
        return;
      }
      if(checkData.length > 1){
        toastrComponent.show("warning", "一次只能选择一个匹配证书！");
        return;
      }
      var signTypeId = checkData[0].id;
      swal({
        title: "你确定要将该证书匹配到当前公司？",
        type: "warning",
        allowOutsideClick: true,
        showConfirmButton: true,
        showCancelButton: true,
        confirmButtonClass: "layui-btn layui-btn-warm",
        cancelButtonClass: "layui-btn layui-btn-primary",
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      }, function (o) {
        if (o) {
          $.ajax({
            type: "get",
            url: "matchPersonSign.do",
            data: {
              "signId": signId,
              "signTypeId": signTypeId,
              "personSignId": personSignId
            },
            success: function (data) {
              if("success" == data){
                toastrComponent.show("success", "匹配成功！");
                initMatchedTable();
                initCanMatchPersonSignTable();
                var searchStr = $("#searchStr").val();
                initTab(searchStr);
                tablerelaod("qyTab");
              }
            }
          });
        }
      });
    });
  })

}

function initMatchedTable(){
  var signId = $("#editSignId").val();
  layui.use(['table'], function() {
    var table = layui.table;
    table.render({
      elem: '#matchedTab'
      , url: 'getMatchedSignList.do?signId=' + signId
      ,page: {
        layout: ['count', 'prev', 'page', 'next', 'skip']
        , groups: 1 //只显示 1 个连续页码
        , first: false //不显示首页
        , last: false //不显示尾页
        , limit: 5
        , limits: [5]
      }
      , cols: [[
        {field: 'name', align: 'center', width: '10%', title: '名称'}
        ,{field: 'bigType', align: 'center', width: '10%', title: '大类'}
        ,{field: 'smallType', align: 'center', width: '13%', title: '小类'}
        , {field: 'unit', align: 'center', width: '5%', title: '年限',templet: function (data) {
          var chUnit;
          if(data.unit == 1){
            chUnit="一年";
          }else if(data.unit == 2){
            chUnit="两年";
          }else if(data.unit == 3){
            chUnit="三年";
          }
          return  chUnit;
        }}
        ,{field: 'purpose', align: 'center', width: '6%', title: '用途'}
        ,{field: 'project', align: 'center', width: '8%', title: 'B证'}
        ,{field: 'initStatus', align: 'center', width: '6%', title: '状态'}
        ,{field: 'price', align: 'center', width: '8%', title: '价格'}
        , {field: 'createTime', align: 'center', width: '9%', title: '录入时间'}
        , {field: 'matchTime', align: 'center', width: '9%', title: '匹配时间'}
        , {field: 'createUserName', align: 'center', width: '8%', title: '创建人'}
        , {field: 'id', align: 'center', width: '8.3%', title: '操作',templet: function (data) {
          var html = '<a class="layui-btn layui-btn-xs cancelMatch" lay-event="edit" personSignId='+data.personSignId+'>移除证书</a>';
          return html;
        }}
      ]]

    });
  })
}
function initCanMatchPersonSignTable(){
  var search = $("#signmatchstr").val();
  layui.use(['table'], function() {
    var table = layui.table;
    table.render({
      elem: '#personTab'
      , url: 'getCanMatchPersonSignCustom.do?search=' + search
      ,page: {
        layout: ['count', 'prev', 'page', 'next', 'skip']
        , groups: 1 //只显示 1 个连续页码
        , first: false //不显示首页
        , last: false //不显示尾页
        , limit: 5
        , limits: [5]
      }
      , cols: [[
        {field: 'name', align: 'center', width: '10%', title: '名称'}
        ,{field: 'bigType', align: 'center', width: '10%', title: '大类'}
        ,{field: 'smallType', align: 'center', width: '10%', title: '小类'}
        , {field: 'unit', align: 'center', width: '5%', title: '年限',templet: function (data) {
          var chUnit;
          if(data.unit == 1){
            chUnit="一年";
          }else if(data.unit == 2){
            chUnit="两年";
          }else if(data.unit == 3){
            chUnit="三年";
          }
          return  chUnit;
        }}
        ,{field: 'purpose', align: 'center', width: '10%', title: '用途'}
        ,{field: 'project', align: 'center', width: '10%', title: 'B证'}
        ,{field: 'initStatus', align: 'center', width: '9%', title: '状态'}
        ,{field: 'price', align: 'center', width: '10%', title: '价格'}
        , {field: 'createTime', align: 'center', width: '10%', title: '录入时间'}
        , {field: 'mark', align: 'center', width: '10%', title: '备注'}
        , {field: 'id', align: 'center', width: '6.5%', title: '操作',templet: function (data) {
          var html = '<a class="layui-btn layui-btn-xs matchPersonSign" lay-event="edit" personSignId='+data.id+'>匹配</a>';
          return html;
        }}
      ]]

    });
  })
}

//搜索按钮事件
function searchBtnInit() {
  $("#signmatchbtn").on("click",function () {
    var serachStr = $("#signmatchstr").val();
    initCanMatchPersonSignTable(serachStr);
  });
}
